package com.jindu.producer.finance.controller;

import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.api.DictionaryInterface;
import com.jindu.producer.finance.jpa.HightechJpa;
import com.jindu.producer.finance.jpa.LegalJpa;
import com.jindu.producer.finance.model.DictionaryEntity;
import com.jindu.producer.finance.model.FHightechEntity;
import com.jindu.producer.finance.model.FLegalEntity;
import com.jindu.producer.finance.model.FLegalQuestionAndAnswer;
import com.jindu.producer.finance.shiro.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "法律评测结果管理", tags = "LegalController", basePath = "/legal")
@RestController
@RequestMapping(value = "/legal",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class LegalController {

    @Autowired
    LegalJpa legalJpa;

    @Autowired
    HightechJpa hightechJpa;

    @Autowired
    DictionaryInterface dictionaryInterface;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据法律评测的ID，查询法律评测的信息", notes = "返回单个法律评测的信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<FLegalEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(legalJpa.findById(id));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据法律评测的ID，查询法律评测的信息", notes = "返回单个法律评测的信息")
    @RequestMapping(value = "getByUserId",method = RequestMethod.GET)
    public JsonResult<FLegalEntity> getByUserId(ServletRequest request){

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String authorization = httpServletRequest.getHeader("Authorization");
        if(authorization!=null) {
            String userId = JwtUtils.getUserId(authorization);
            return JsonResult.getSuccessResult(legalJpa.findByUserId(userId));
        }else {
            return JsonResult.getFailureByNoAuthorization();
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据企业的ID，查询法律评测的信息", notes = "返回单个法律评测的信息")
    @RequestMapping(value = "getByOrgId",method = RequestMethod.GET)
    public JsonResult<List<FLegalQuestionAndAnswer>> getByOrgId(@RequestParam Integer orgId){


        List<FLegalQuestionAndAnswer> QAW= new ArrayList<>();

        List<DictionaryEntity> question=dictionaryInterface.getListByParentId("c3f2a8b8-758b-48f9-8730-67d66017452b");
        List<FLegalEntity> answer=legalJpa.findByOrgId(orgId);

        for (DictionaryEntity q : question) {
            FLegalQuestionAndAnswer fq=new FLegalQuestionAndAnswer();

            fq.setSortId(q.getSortId());
            fq.setQuestionName(q.getName());

            for (FLegalEntity as : answer) {
                if(as.getQuestionId()==q.getSortId()){
                    fq.setId(as.getId());
                    fq.setOrgId(as.getOrgId());
                    fq.setQuestionId(as.getQuestionId());
                    fq.setAnswer(as.getAnswer());
                }
            }
            QAW.add(fq);
        }
        return JsonResult.getSuccessResult(QAW);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存国高评测的信息", notes = "保存国高评测的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FLegalEntity> save(@RequestBody FLegalEntity legalEntity){

        FLegalEntity fLegalEntity=legalJpa.findById(legalEntity.getId());

        FHightechEntity hightechEntity= hightechJpa.findByOrgId(legalEntity.getOrgId());

        if(hightechEntity==null){
            return JsonResult.getFailureResult();
        }
        if(legalEntity.getQuestionId()==null|| legalEntity.getAnswer()==null){
            return JsonResult.getFailureResult();
        }
        if(fLegalEntity == null){
            legalEntity.setId(IdGenerator.uuid());
            legalEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            legalEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(legalJpa.saveAndFlush(legalEntity));
        }else {
            legalEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(legalEntity,fLegalEntity, AlphaBeanUtils.getNullPropertyNames(legalEntity));
            return JsonResult.getSuccessResult(legalJpa.saveAndFlush(fLegalEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除法律评测的信息", notes = "根据ID,删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(legalJpa.deleteById(id));
    }


}
